package com.elitel.common.core.domain;

import lombok.*;

import java.io.Serializable;

/**
 * 返回结果封装
 *
 * @author mulis
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1550436928385370645L;

    private Integer code;

    private String msg;

    private boolean success;

    private T data;


    public static JsonResult<?> success() {
        JsonResult<?> jsonResult = new JsonResult<>();
        jsonResult.setCode(Codes.SUCCESS.getCode());
        jsonResult.setMsg(Codes.SUCCESS.getMessage());
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    public static <T> JsonResult<T> success(T o) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setCode(Codes.SUCCESS.getCode());
        jsonResult.setMsg(Codes.SUCCESS.getMessage());
        jsonResult.setSuccess(true);
        jsonResult.setData(o);
        return jsonResult;
    }

    public static JsonResult<?> error() {
        JsonResult<?> jsonResult = new JsonResult<>();
        jsonResult.setCode(Codes.ERROR.getCode());
        jsonResult.setMsg(Codes.ERROR.getMessage());
        jsonResult.setSuccess(false);
        jsonResult.setMsg("服务器内部异常");
        return jsonResult;
    }

    public static JsonResult<?> error(String errorMsg) {
        JsonResult<?> jsonResult = new JsonResult<>();
        jsonResult.setCode(Codes.ERROR.getCode());
        jsonResult.setSuccess(false);
        jsonResult.setMsg(errorMsg);
        return jsonResult;
    }

    public static JsonResult<?> error(String errorMsg, Integer code) {
        JsonResult<?> jsonResult = new JsonResult<>();
        jsonResult.setCode(code);
        jsonResult.setSuccess(false);
        jsonResult.setMsg(errorMsg);
        return jsonResult;
    }

    public static <T> JsonResult errorAddData(String errorMsg, Integer code, T messageData) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setCode(code);
        jsonResult.setSuccess(false);
        jsonResult.setMsg(errorMsg);
        jsonResult.setData(messageData);
        return jsonResult;
    }

    public static JsonResult<?> warn(String message) {
        JsonResult<?> res = new JsonResult<>();
        res.setCode(Codes.WARNING.getCode());
        res.setMsg(message);
        res.setSuccess(false);
        return res;
    }

    public static JsonResult<?> warn() {
        JsonResult<?> res = new JsonResult<>();
        res.setCode(Codes.WARNING.getCode());
        res.setMsg(Codes.WARNING.getMessage());
        res.setSuccess(false);
        return res;
    }


    @Getter
    public enum Codes {
        /**
         * 成功
         */
        SUCCESS(200, "操作成功"),
        /**
         * 警告
         */
        WARNING(301, "警告"),
        /**
         * 失败
         */
        ERROR(500, "服务器内部错误"),

        /**
         * 无权限
         */
        NO_PERM(403, "服务器内部错误");

        private final int code;
        private final String message;

        Codes(int code, String message) {
            this.code = code;
            this.message = message;
        }

    }
}
